package com.alex.j2se.serializable;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;

/**
 * Author  : gaojun            <br/>
 * Date    : 2020/8/5                          <br/>
 * Time    : 12:43 下午                         <br/>
 * ---------------------------------------    <br/>
 * Desc    :
 */
public class SerializableTest {
    public static void main(String[] args) {
//        serializableTest();
        unserializableTest2();
    }

    public static void serializableTest() {
        TargetObj targetObj = new TargetObj();
        targetObj.setI1(1);
        targetObj.setS1("str1");
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/mtdp/target.log"))){
            objectOutputStream.writeObject(targetObj);
            objectOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        unserializableTest1();
    }

    private static void unserializableTest1() {
        try (ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("/Users/mtdp/target.log"))){
            TargetObj targetObj = (TargetObj)ois1.readObject();
            System.out.println(targetObj);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void unserializableTest2() {
        try (ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("/Users/mtdp/target.log"))){
            TargetObj targetObj = (TargetObj)ois1.readObject();
            System.out.println(targetObj);
            System.out.println(targetObj.getAttributeObj());
            targetObj.method2();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
